package com.etop.learnapp.fragment;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.etop.learnapp.R;
import com.etop.learnapp.activity.ApplyActivity;
import com.etop.learnapp.personalClass.Teacher;

import java.util.ArrayList;


public class Teacher_Info_Fragment extends Fragment {

    View view;
    Button btnComplete_Edit1,btn_teacherInfoBack;
    ApplyActivity applyActivity;
    private ArrayList<Teacher> teacherArrayList;
    private EditText etTeacherName,etTeacherSex,etTeacherEmail,etTeacherPhone,etTeacherID,etTeacherRemarks,etTeacherSoftware1,etTeacherSoftware2,etTeacherSoftware3;
    int position;

    public void setPosition(int position) {
        this.position = position;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        applyActivity = (ApplyActivity) this.getActivity();
        teacherArrayList = applyActivity.teacherArrayList;

    }

    private void init(View view){
        btnComplete_Edit1 = (Button)view.findViewById(R.id.btn_complete_Edit);
        btn_teacherInfoBack = (Button)view.findViewById(R.id.btn_teacherInfoBack);
        btn_teacherInfoBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                applyActivity.setCurrentPage(2);
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fl_Main, applyActivity.teacher_list_fragment);
                fragmentTransaction.addToBackStack(null);//
                fragmentTransaction.commit();
            }
        });

        etTeacherName = (EditText)view.findViewById(R.id.etTeacherName);
        etTeacherSex  = (EditText)view.findViewById(R.id.etTeacherSex);
        etTeacherEmail  = (EditText)view.findViewById(R.id.etTeacherEmail);
        etTeacherPhone  = (EditText)view.findViewById(R.id.etTeacherPhone);
        etTeacherID  = (EditText)view.findViewById(R.id.etTeacherID);
        etTeacherRemarks  = (EditText)view.findViewById(R.id.etTeacherRemarks);
        etTeacherSoftware1  = (EditText)view.findViewById(R.id.etTeacherSoftware1);
        etTeacherSoftware2  = (EditText)view.findViewById(R.id.etTeacherSoftware2);
        etTeacherSoftware3  = (EditText)view.findViewById(R.id.etTeacherSoftware3);
    }

    private void show(){
        if (teacherArrayList.get(position) != null){

            etTeacherName.setText(teacherArrayList.get(position).getName().toString());
            etTeacherSex.setText(teacherArrayList.get(position).getSex().toString());
            etTeacherEmail.setText(teacherArrayList.get(position).getEmail().toString());
            etTeacherPhone.setText(teacherArrayList.get(position).getPhone().toString());
            etTeacherID.setText(teacherArrayList.get(position).getID().toString());
            etTeacherRemarks.setText(teacherArrayList.get(position).getRemarks().toString());
            etTeacherSoftware1.setText(teacherArrayList.get(position).getSoftware_1().toString());
            etTeacherSoftware2.setText(teacherArrayList.get(position).getSoftware_2().toString());
            etTeacherSoftware3.setText(teacherArrayList.get(position).getSoftware_3().toString());
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_teacher__info, container, false);
        init(view);
        show();

        btnComplete_Edit1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                //
                if (teacherArrayList.get(position) == null) applyActivity.ExtendTeacherList();

                teacherArrayList.get(position).setName(etTeacherName.getText().toString());
                teacherArrayList.get(position).setSex(etTeacherSex.getText().toString());
                teacherArrayList.get(position).setEmail(etTeacherEmail.getText().toString());
                teacherArrayList.get(position).setPhone(etTeacherPhone.getText().toString());
                teacherArrayList.get(position).setID(etTeacherID.getText().toString());
                teacherArrayList.get(position).setRemarks(etTeacherRemarks.getText().toString());
                teacherArrayList.get(position).setSoftware_1(etTeacherSoftware1.getText().toString());
                teacherArrayList.get(position).setSoftware_2(etTeacherSoftware2.getText().toString());
                teacherArrayList.get(position).setSoftware_3(etTeacherSoftware3.getText().toString());

                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fl_Main, ((ApplyActivity) getActivity()).teacher_list_fragment);
                fragmentTransaction.commit();
            }
        });

        return view;
    }

}
